package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.vo.*;
import com.xueyi.aixihu.reqeust.AuditNewsReq;
import com.xueyi.aixihu.reqeust.FindWaitAuditNewsListReq;
import com.xueyi.aixihu.reqeust.OpenNewsReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author hxs
 * @createDate 2023/6/28 10:05
 * @description 文章审核接口
 */
public interface NewAuditMapper {

    /**
     * 审核通过镇级党务公开
     *
     * @param auditNewsReq
     */
    @Update(" UPDATE dt_news SET audit_status = #{req.auditStatus} WHERE id = #{req.id} ")
    void auditSuccessNewsByZJDW(@Param("req") AuditNewsReq auditNewsReq);

    /**
     * 审核不通过镇级政务公开
     *
     * @param auditNewsReq
     */
    @Update(" UPDATE dt_news SET audit_status = #{req.auditStatus},fail_msg = #{req.failMsg} WHERE id = #{req.id} ")
    void auditFailNewsByZJZW(@Param("req") AuditNewsReq auditNewsReq);

    /**
     * 审核通过镇级政务公开
     *
     * @param auditNewsReq
     */
    @Update(" UPDATE dt_news SET audit_status = #{req.auditStatus} WHERE id = #{req.id} ")
    void auditSuccessNewsByZJZW(@Param("req") AuditNewsReq auditNewsReq);

    /**
     * 审核不通过镇级党务公开
     *
     * @param auditNewsReq
     */
    @Update(" UPDATE dt_news SET audit_status = #{req.auditStatus},fail_msg = #{req.failMsg} WHERE id = #{req.id} ")
    void auditFailNewsByZJDW(@Param("req") AuditNewsReq auditNewsReq);

    /**
     * 审核通过镇级党务公开的日志
     *
     * @param auditNewsReq
     * @param wzZjDwAudit
     * @param id
     */
    @Update(" UPDATE cdz_audit_record SET " +
            " current_status = #{req.auditStatus},last_command_time = NOW() " +
            " WHERE join_id = #{req.id} AND audit_type = #{wzZjDwAudit} ")
    void addAuditSuccessNewsByZJDWRecord(@Param("req") AuditNewsReq auditNewsReq,
                                         @Param("wzZjDwAudit") Integer wzZjDwAudit,
                                         @Param("id") Integer id);

    /**
     * 审核不通过镇级党务公开的日志
     *
     * @param auditNewsReq
     * @param wzZjDwAudit
     * @param id
     */
    @Update(" UPDATE cdz_audit_record SET " +
            " current_status = #{req.auditStatus},last_command_time = NOW(),fail_msg = #{req.failMsg} " +
            " WHERE join_id = #{req.id} AND audit_type = #{wzZjDwAudit} ")
    void addAuditFailNewsByZJDWRecord(@Param("req") AuditNewsReq auditNewsReq,
                                      @Param("wzZjDwAudit") Integer wzZjDwAudit,
                                      @Param("id") Integer id);

    /**
     * 是否公开镇级党务公开
     *
     * @param openNewsReq
     * @return
     */
    @Update(" UPDATE dt_news SET is_public = #{req.isPublic} WHERE id = #{req.id} ")
    void openNewsByZJDW(@Param("req") OpenNewsReq openNewsReq);

    /**
     * 同步更改审核记录的是否公开镇级党务公开
     *
     * @param openNewsReq
     * @param wzZjDwAudit
     */
    @Update(" UPDATE cdz_audit_record SET is_public = #{req.isPublic},last_command_time = NOW() " +
            " WHERE join_id = #{req.id} AND audit_type = #{wzZjDwAudit} ")
    void openNewsByZJDWRecord(@Param("req") OpenNewsReq openNewsReq,
                              @Param("wzZjDwAudit") Integer wzZjDwAudit);

    /**
     * 是否公开镇级政务公开
     *
     * @param openNewsReq
     * @return
     */
    @Update(" UPDATE dt_news SET is_public = #{req.isPublic} WHERE id = #{req.id} ")
    void openNewsByZJZW(@Param("req") OpenNewsReq openNewsReq);

    /**
     * 同步更改审核记录的是否公开镇级党务公开
     *
     * @param openNewsReq
     * @param wzZjZwAudit
     */
    @Update(" UPDATE cdz_audit_record SET is_public = #{req.isPublic},last_command_time = NOW() " +
            " WHERE join_id = #{req.id} AND audit_type = #{wzZjZwAudit} ")
    void openNewsByZJZWRecord(@Param("req") OpenNewsReq openNewsReq,
                              @Param("wzZjZwAudit") Integer wzZjZwAudit);

    /**
     * 审核通过镇级政务公开的日志
     *
     * @param auditNewsReq
     * @param wzZjZwAudit
     * @param id
     */
    @Update(" UPDATE cdz_audit_record SET " +
            " current_status = #{req.auditStatus},last_command_time = NOW() " +
            " WHERE join_id = #{req.id} AND audit_type = #{wzZjZwAudit} ")
    void addAuditSuccessNewsByZJZWRecord(@Param("req") AuditNewsReq auditNewsReq,
                                         @Param("wzZjZwAudit") Integer wzZjZwAudit,
                                         @Param("id") Integer id);

    /**
     * 审核不通过镇级政务公开的日志
     *
     * @param auditNewsReq
     * @param wzZjZwAudit
     * @param id
     */
    @Update(" UPDATE cdz_audit_record SET " +
            " current_status = #{req.auditStatus},last_command_time = NOW(),fail_msg = #{req.failMsg} " +
            " WHERE join_id = #{req.id} AND audit_type = #{wzZjZwAudit} ")
    void addAuditFailNewsByZJZWRecord(@Param("req") AuditNewsReq auditNewsReq,
                                      @Param("wzZjZwAudit") Integer wzZjZwAudit,
                                      @Param("id") Integer id);

    /**
     * 查询待审核文章列表
     *
     * @param findWaitAuditNewsListReq 参数
     * @param areaIds                  地区ids
     * @return 待审核文章列表
     */
    @Select({
            " <script>" +
                    "    select dn.* , du.username , dns.`name` areaName , tab.id auditId , du.avatar userPhoto , cst.name systemTypeName from (   " +
                    "    select * from cdz_audit_record where audit_type = #{item.auditType} and is_valid = 1 " +
                    "   <if test = 'item.currentStatus != null'>" +
                    "  and current_status in (${item.currentStatusStr})  " +
                    "   </if> " +
                    "   <if test = 'item.month!=null'>" +
                    "    and DATE_FORMAT(submit_time,'%Y-%m') = #{item.month}   " +
                    "   </if>   " +
                    "   ) tab left join dt_news dn on tab.join_id = dn.id    " +
                    "   left join dt_users du on tab.submit_user_id = du.id   " +
                    "   left join dt_news_sort dns on dn.from_to_area_id = dns.id " +
                    "   left join cdz_system_type cst on dn.system_type_id = cst.id   " +
                    "   where dn.from_to_area_id in (${areaIds}) " +
                    "   <if test = 'item.keyword != null'>" +
                    "   and dn.title like '%${item.keyword}%' " +
                    "   </if>   " +
                    "   <if test = 'item.systemTypeId != null'>" +
                    "   and dn.cid = #{item.systemTypeId}  " +
                    "   </if> " +
                    "   <if test = 'item.twoIndex != null'>" +
                    "   and dn.from_to_area_id = #{item.twoIndex}  " +
                    "   </if> " +
                    "   and dn.is_valid = 1   " +
                    "   order by  tab.submit_time    " +
                    "   limit #{item.startIndex} , #{item.limit}   " +
                    " </script> "
    })
    List<WaitAuditNewsVo> findWaitAuditNewsList(@Param("item") FindWaitAuditNewsListReq findWaitAuditNewsListReq, @Param("areaIds") String areaIds);

    /**
     * 查询待审核文章列表总条数
     *
     * @param findWaitAuditNewsListReq 参数
     * @param areaIds                  地区ids
     * @return 总条数
     */
    @Select({
            " <script>" +
                    "    select count(dn.id)  from (   " +
                    "    select * from cdz_audit_record where audit_type = #{item.auditType} and is_valid = 1 " +
                    "   <if test = 'item.currentStatus != null'>" +
                    "  and current_status in (${item.currentStatusStr})  " +
                    "   </if>    " +
                    "   <if test = 'item.month!=null'>" +
                    "    and DATE_FORMAT(submit_time,'%Y-%m') = #{item.month}   " +
                    "   </if>   " +
                    "   ) tab left join dt_news dn on tab.join_id = dn.id    " +
                    "   left join dt_users du on tab.submit_user_id = du.id   " +
                    "   left join dt_news_sort dns on dn.from_to_area_id = dns.id   " +
                    "   where dn.from_to_area_id in (${areaIds}) " +
                    "   <if test = 'item.keyword != null'>" +
                    "   and dn.title like '%${item.keyword}%' " +
                    "   </if>   " +
                    "   <if test = 'item.systemTypeId != null'>" +
                    "   and dn.cid = #{item.systemTypeId}  " +
                    "   </if> " +
                    "   <if test = 'item.twoIndex != null'>" +
                    "   and dn.from_to_area_id = #{item.twoIndex}  " +
                    "   </if> " +
                    "   and dn.is_valid = 1   " +
                    " </script> "
    })
    Integer findWaitAuditNewsListTotal(@Param("item") FindWaitAuditNewsListReq findWaitAuditNewsListReq, @Param("areaIds") String areaIds);

    /**
     * 通过文章id查询文章
     *
     * @param id id
     * @return
     */
    @Select({
            " select * from dt_news where is_valid = 1 and id = #{id} "
    })
    DtNewsVo findNewById(@Param("id") Integer id);

    /**
     * 修改文章的状态
     *
     * @param dtNewsVo 文章对象
     */
    @Update({
            " update dt_news set audit_status = #{item.auditStatus} , fail_msg = #{item.failMsg}  where id = #{item.id} "
    })
    void updateNewAuditStatus(@Param("item") DtNewsVo dtNewsVo);

    /**
     * 修改文章审核记录状态
     *
     * @param auditId     审核记录id
     * @param auditStatus 审核状态
     * @param failMsg     拒绝理由
     */
    @Update({
            " update cdz_audit_record set last_command_time = now() , fail_msg = #{failMsg} , current_status = #{auditStatus} where id = #{auditId} "
    })
    void updateNewAuditRecordStatus(@Param("auditId") Integer auditId, @Param("auditStatus") Integer auditStatus, @Param("failMsg") String failMsg);

    /**
     * 查询待审核文章数据
     *
     * @param contentType       类型
     * @param commissionSizeStr 代办
     * @param doneSizeStr       已完成
     * @return
     */
    @Select({
            "   select   " +
                    "    count(IF(audit_status in (${commissionSizeStr}),id,null)) commissionSize,  " +
                    "    count(IF(audit_status in (${doneSizeStr}),id,null)) doneSize  " +
                    "   from dt_news where is_valid = 1 and content_type = #{contentType} "
    })
    FindWaitAuditNewsSizeVo findWaitAuditNewsSize(@Param("contentType") Integer contentType, @Param("commissionSizeStr") String commissionSizeStr, @Param("doneSizeStr") String doneSizeStr);


    /***
     * 查询日志
     * @param id
     * @return
     */
    @Select("SELECT a1.* , du.username username , af.area_type  FROM (" +
            " SELECT * FROM axh_finance_log WHERE finance_id = #{id} AND current_finance_status = #{type} and log_type = #{logType} ORDER BY create_time DESC LIMIT 1" +
            " ) a1 LEFT JOIN dt_users du ON a1.command_user_id = du.id " +
            " LEFT JOIN axh_finance af ON a1.finance_id = af.id ")
    AxhAuditFinanceLogVo findNowsLog(@Param("id") Integer id, @Param("type") Integer type, @Param("logType") Integer logType);

    /**
     * 查询待审核文章对象
     *
     * @param id 文章id
     * @return 待审核文章对象
     */
    @Select({
            "select tab.* , du.username , dns.`name` areaName , du.avatar userPhoto , cst.`name` systemTypeName from (    " +
                    "    select * from dt_news where id = #{id} and is_valid = 1    " +
                    ") tab     " +
                    "left join dt_users du on tab.create_user_id = du.id    " +
                    "left join dt_news_sort dns on tab.from_to_area_id = dns.id " +
                    "left join cdz_system_type cst on tab.system_type_id = cst.id    "
    })
    WaitAuditNewsVo findNewsVoById(@Param("id") Integer id);

    @Select({
            " select id value , name label ,pid from dt_news_sort where is_valid = 1  "
    })
    List<BaseSelectVo> findClassifyList();
}
